package com.inossem.warehousemanage_tv.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.warehousemanage_tv.MainActivity;
import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.bean.PositionsBean;
import com.inossem.warehousemanage_tv.utils.ToastUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 仓库概览-仓位-层
 *
 * @author 詹建宇
 * @time on 2019/2/15 11:43
 * @email jianyu.zhan@inossem.com
 */
public class PositionsLayerAdapter {

    private MainActivity activity;
    private LinearLayout linearLayout;
    private List<PositionsBean.LayerListBean> list;
    private OnItemClickListener listener;
    private List<TextView> views;
    private int position;

    public PositionsLayerAdapter(MainActivity activity, LinearLayout linearLayout, List<PositionsBean.LayerListBean> list, OnItemClickListener listener) {
        this.activity = activity;
        this.linearLayout = linearLayout;
        this.list = list;
        this.listener = listener;

        addView();
    }

    private void addView() {
        linearLayout.removeAllViews();
        views = new ArrayList<>();
        position = 0;

        for (int i = 0; i < list.size(); i++) {
            PositionsBean.LayerListBean bean = list.get(i);
            View convertView = LayoutInflater.from(activity).inflate(R.layout.view_ware_positions_layer_item, null);
            convertView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f));
            TextView tv = convertView.findViewById(R.id.tv);
            tv.setText(bean.getPosition_index2());
            tv.setTag(i);

            if (this.position == i) {
                tv.setBackgroundResource(R.color.base_blue);
                tv.setTextColor(activity.getResources().getColor(R.color.base_white));
            } else {
                tv.setBackgroundResource(R.drawable.ic_bg_1);
                tv.setTextColor(activity.getResources().getColor(R.color.base_font));
            }

            tv.setOnClickListener(v -> {
                if (position == (int) v.getTag()) return;

                position = (int) v.getTag();

                for (TextView view : views) {
                    view.setBackgroundResource(R.drawable.ic_bg_1);
                    view.setTextColor(activity.getResources().getColor(R.color.base_font));
                }

                v.setBackgroundResource(R.color.base_blue);
                ((TextView) v).setTextColor(activity.getResources().getColor(R.color.base_white));

                listener.onItemClick(position, bean);
            });

            linearLayout.addView(convertView);
            views.add(tv);
        }

    }

    public interface OnItemClickListener {
        void onItemClick(int position, PositionsBean.LayerListBean layerBean);
    }

}
